<script type="text/javascript">

  //限制输入数字，只能输入两位
  function checkNum(obj) {
    //检查是否是非数字值
    if (isNaN(obj.value)) {
      obj.value = "0.00";
      return;
    }
    if (obj != null) {
      //没有小数时自动补零
      if (obj.value.indexOf('.') == -1) {
        if (obj.value == "") {
          obj.value = "0.00";
        } else {
          obj.value += ".00";
        }
      }
      if (obj.value.toString().split(".")[1].length < 2) {
        if (obj.value.toString().split(".")[1].length == 0) {
          obj.value += "00";
        }
        if (obj.value.toString().split(".")[1].length == 1) {
          obj.value += "0";
        }
      }
      //检查小数点后是否对于两位
      if (obj.value.toString().split(".").length > 1 &&
        obj.value.toString().split(".")[1].length > 2) {
        obj.value = obj.value.substring(0, obj.value.indexOf('.') + 3);
      }
    }
  }

  function checkNumForEnter(obj) {
    //回车事件拦截
    if (event.keyCode == 13) {
      checkNum(obj);
    }
  }

</script>
<div id="memberMain" class="success">
  <div class="title"><h4>增值消费</h4></div>
  <div class="tipsTop">今日赠送本金的<span id="interest"></span>%积分，10天一轮，一轮结束本息可一次性提现</div>
  <div class="height18"></div>
  <form autocomplete="off" id="recharge-form">
    <ul class="formUl" id="userBank">
      <li class="">
        <label class="inputName">会员编号</label>
        <input class="inputText cardName" name="username" value="" id="username" type="text"
               style="color: rgb(0, 0, 0);" disabled="">
      </li>
      <li class="showCss liHover">
        <label class="inputName">积分类型</label>
        <input class="inputText cardName" id="originalStyle" name="originalStyle" value="原始积分(0.00)" type="text"
               style="color: rgb(0, 0, 0);" disabled="">
        <input class="inputText cardName" id="originalPoint" name="originalPoint" value="0.00" type="text"
               style="color: rgb(0, 0, 0);" onblur="checkNum(this)" onkeypress="checkNumForEnter(this)">
      </li>
      <li class="">
        <label class="inputName"></label>
        <input class="inputText cardName" id="repeatStyle" name="repeatStyle" value="复投积分(0.00)" type="text"
               style="color: rgb(0, 0, 0);" disabled="">
        <input class="inputText cardName" name="repeatPoint" value="0.00" id="repeatPoint" type="text"
               style="color: rgb(0, 0, 0);" onblur="checkNum(this)" onkeypress="checkNumForEnter(this)">
      </li>
      <li class="">
        <label class="inputName">投入积分</label>
        <select name="investAmount" id="investAmount">
          <option value="0">请选择额度</option>
          <option value="2000">投入2000积分</option>
          <option value="5000">投入5000积分</option>
          <option value="10000">投入10000积分</option>
          <option value="20000">投入20000积分</option>
        </select>
      </li>
      <li class="">
        <label class="inputName">交易密码</label>
        <input class="inputText" minlength="6" maxlength="16" name="pay_pwd" id="pay_pwd" type="text"
               style="color: rgb(0, 0, 0);">
      </li>
    </ul>
    <ul class="formUl">
      <li>
        <input class="inputBut showL inputBtnBig" type="button" value="确认" id="jax">
      </li>
    </ul>
  </form>
</div>

<script type="text/javascript">
  $(function () {

    $("*[name='pay_pwd']").focus(function () {
      $(this).attr('type', 'password');
    })
    $('#jax').bind('click', buy);

    function buy() {

      $('body').mLoading('show');

      var originalPoint = $('#originalPoint').val(),
        repeatPoint = $('#repeatPoint').val();
      if (isNaN(originalPoint))
        originalPoint = 0;
      else
        originalPoint = parseFloat(originalPoint);
      if (isNaN(repeatPoint))
        repeatPoint = 0;
      else
        repeatPoint = parseFloat(repeatPoint);
      if ((isNaN(originalPoint) || originalPoint <= 0 )
        && (isNaN(repeatPoint) || repeatPoint <= 0)) {
        error('请输入有效的投入额度', function () {
        });
        $('body').mLoading('hide');
        return false;
      }
      var investAmount = $('#investAmount').val();
      if (investAmount == 0) {
        error('请选择一个消费额度', function () {
        })
        $('body').mLoading('hide');
        return false;
      } else {
        investAmount = parseFloat(investAmount);
      }
      if (parseFloat(originalPoint + repeatPoint) != parseFloat(investAmount)) {
        error('请输入有效的投入额度', function () {
        });
        $('body').mLoading('hide');
        return false;
      }
      var info = '你正在消费的金额为:' + investAmount + ',是否确认消费?';
      confirm(info, function () {

        $.post('/transaction/invest', {
          payPassword: $.md5($('#pay_pwd').val()),
          ruleId: 1,
          originalPoint: originalPoint,
          repeatPoint: repeatPoint,
          investPointAmount: investAmount,
        }, function (data) {
          if (data.retCode == 1) {

            $('body').mLoading('hide');
            success(data.retMsg, function () {
              location.reload();
            })
          } else {
            $('body').mLoading('hide');
            error(data.retMsg, function () {
            })
          }
        })
      })
    }

    function getPoint() {
      $.post('/transaction/getPoint', {}, function (data) {
        if (data.retCode == 1) {
          $('#originalStyle').val("原始积分(" + data.data.cpOriginalPoint + ")");
          $('#repeatStyle').val("复投积分(" + data.data.cpRepeatPoint + ")");
        } else {
          $('#originalStyle').val("原始积分(0.00)");
          $('#repeatStyle').val("复投积分(0.00)");
        }
      })
    }

    function getUser() {
      $.post('/user/currentUser', {}, function (data) {
        if (data.retCode == 1) {
          $('#username').val(data.data.userName);
        } else {
          $('#username').val("");
        }
      })
    }

    function getInterest() {
      $.post('/transaction/getTodayInterest', {ruleId: 1}, function (data) {
        if (data.retCode == 1) {
          $('#interest').html(data.data.interest);
        } else {
        }
      })
    }

    getUser();
    getPoint();
    getInterest();

  })
</script>
